package com.example.demo.service;

import com.example.demo.Repository.UserRepository;
import com.example.demo.pojo.DTO.UserDTO;
import com.example.demo.pojo.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {

    @Autowired
    UserRepository userRepository;
    @Override
    public User add(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        return userRepository.save(user);

    }


    /**
     * 查询用户
     * @param id
     * @return
     */
    @Override
    public User get(Integer id) {
         return userRepository.findById(id).orElseThrow(()-> new IllegalArgumentException("用户不存在或参数错误"));
    }

    @Override
    public User edit(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        return userRepository.save(user);
    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);
    }
}
